Welcome to Accessing MongoDB from Python.
After watching this video, you will be able to: Explain what the MongoClient is,
And perform basic CRUD operations for our Campus Management database using Python.
The MongoClient is a class that helps you interact with MongoDB.
First, we import MongoClient from pymongo, which is an official MongoDB driver for Python.
The ‘uri’ tells us the address where MongoDB is. Then we create MongoClient, which we
should only do once in our code. The next part gets the object pointing
to the campus management database, And finally, from the campus management database
object, we point to the ‘students’ collection. Now that we have a reference
to the ‘students’ collection, Let’s create a student document.
For this, we call the ‘insert_one’ function, which takes the document
we are trying to insert as its input. We can also do a bulk insert by
calling the ‘insert_many’ function. This function takes a list instead as its input.
Now let’s do some Read operations. In the first one, we call ‘find_one’, without
any filter criteria. This will return the first document in natural order, which is the order in
which the database refers to documents on disk. Then we find the first student with an email.
If more than one document matches the criteria, only the first one will be read, because
we are using the ‘find_one’ function. To get all the documents, we do a
‘find’. Now we are retrieving all students with the lastname of Doe.
And finally, if we just want to count how many students there are with the
lastname Doe, we call the ‘count’ function. This count will happen on MongoDB, and only the
number value will be returned to the client. When we do a ‘find’ on a collection
in MongoDB, we get a cursor back. This cursor points to our documents
in MongoDB. If you have run this using mongo shell, the shell retrieves those
documents for you behind the scenes. To retrieve documents from our
cursor, we call python ‘dumps’, which will exhaust our cursor
to retrieve all the documents. Let’s retrieve a student and make
some changes to the document. We are creating a new field to identify
whether students are on campus or online only. We will also update the email
with the campus email address. Once all changes are made, we
will call the ‘replace’ function, Which takes its first argument as the filter
criteria to find a document with last name of Doe, And the second argument is
the updated python object. In the Replace example, we were sending the whole
document back with the new changes. For larger documents, this will take a lot of transfer
time between the client and the database. We can use MongoDB’s in-place
updates for small changes. We do this by constructing a change document,
which in our case only has two assignments, which come under ‘$set’,
And with this in-place update, we don’t even need to retrieve the student document.
To update all of the students to online only due to lockdown, we can run the ‘update_many’
function without any filter criteria, and change all documents.
Just like update and find, the ‘delete_one’ and ‘delete_many’ functions
take filter criteria as the first argument, And these functions will delete
one or many documents in the given collection matching the criteria.
In this video, you learned that: MongoClient is a class that
helps you interact with MongoDB. MongoClient is imported from pymongo,
the official MongoDB driver for Python. You can perform single or bulk inserts.
You can replace whole documents. You can perform an in-place update,
which is a preferred option. And you can delete one or more
documents from your collection.